function isEmail(value) {
    return /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(value);
}
function isNumeric(num) {
    return !isNaN(num);
}

function clean() {
    $('#newPasswordField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#nameField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#identityNumberField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#emailField').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#countryDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
    $('#addressDropDown').parent().removeClass('has-error has-feedback').find('span').remove();
}

function validate() {
    clean();
    var validator = {valid: false};
    var newPasswordField = $('#newPasswordField').val();
    if (newPasswordField.length > 70) {
        $('#newPasswordField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Password must have 1-70 characters!</span>').insertAfter('#newPasswordField');
        return validator;
    }


    var nameField = $('#nameField').val();
    if (!nameField) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }
    if (nameField.length > 70) {
        $('#nameField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Name must have 1-100 characters!</span>').insertAfter('#nameField');
        return validator;
    }


    var identityNumberField = $('#identityNumberField').val();
    if (!identityNumberField) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (identityNumberField.length > 11 || identityNumberField.length < 8) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }
    if (!isNumeric(identityNumberField)) {
        $('#identityNumberField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Identity number must have 8-11 numbers!</span>').insertAfter('#identityNumberField');
        return validator;
    }

    var emailField = $('#emailField').val();
    if (!emailField) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (emailField.length > 70) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Email must have 1-60 characters!</span>').insertAfter('#emailField');
        return validator;
    }
    if (!isEmail(emailField)) {
        $('#emailField').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid Email adress!</span>').insertAfter('#emailField');
        return validator;
    }

    if ($("#countryDropDown").val() === "-1") {
        $('#countryDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid Country!</span>').insertAfter('#countryDropDown');
        return validator;
    }

    if ($("#addressDropDown").val() === "-1") {
        $('#addressDropDown').parent().addClass('has-error has-feedback');
        $('<span class="help-block"><span class="glyphicon glyphicon-remove"></span> Not a valid address!</span>').insertAfter('#addressDropDown');
        return validator;
    }

    validator["passwordField"] = newPasswordField;
    validator["nameField"] = nameField;
    validator["identityNumberField"] = identityNumberField;
    validator["emailField"] = emailField;
    validator["id_country"] = $("#countryDropDown").val();
    validator["id_address"] = $("#addressDropDown").val();

    validator["valid"] = true;
    return validator;
}

function onClickCancelUserAccount(eventObject) {
    location.href = '/Trabalho_PD/cancelaccount.html';
}

function onClickEditUserAccount(eventObject) {
    var validator = validate();
    if (validator["valid"]) {
        sendEditUser(validator, function (data) {
            if (data["success"] === true) {
                window.location = '/Trabalho_PD/homepage.html';
            } else {
                $("#registerUserButtonArea").append('<span class="glyphicon glyphicon-remove form-control-feedback"></span><span class="help-block">Error! Retry please!</span>');
            }
        });
    }
}

function loadCountrys() {
    getAllCountrys(function (arrayCountry) {
        for (var i in arrayCountry) {
            $("#countryDropDown").append('<option value="' + arrayCountry[i].id_country + '">' + arrayCountry[i].name + '</option>');
        }
    });
}

function loadAdress(clear) {
    getAllAdressFromCountry($("#countryDropDown").val(), function (arrayAdress) {
        if (clear) {
            $('#addressDropDown')
                    .find('option')
                    .remove()
                    .end()
                    .append('<option value="-1">Select a address</option>');

            for (var i in arrayAdress) {
                $("#addressDropDown").append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
            }
        } else {
            for (var i in arrayAdress) {
                $("#addressDropDown").append('<option value="' + arrayAdress[i].id_address + '">' + arrayAdress[i].address + '</option>');
            }
        }
    });
}

$(document).ready(function () {
    loadCountrys();
    loadAdress(false);
    $("#countryDropDown").change(function () {
        if ($("#countryDropDown").val() !== "-1") {
            loadAdress(true);
        }
    });
});

